<?php
class Api_Sync_Category {
	protected $_data = null;
	protected $_file = null;
	protected $_elements = null;
	
	protected function _loadFromXmlFile($xml_file_name) {
		if (! file_exists ( $xml_file_name ))
			throw new Exception ( 'Can`t find input file: ' . $xml_file_name );
		try {
			$_data = simplexml_load_file ( $xml_file_name );
			foreach ( $_data as $category ) {
				try {
					array_push ( $this->_elements, new Api_Row_Catsync ( $category ) );
				} catch ( Exception $e ) {
					throw new Exception ( 'Xml structure error' );
				}
			}
		} catch ( Exception $e ) {
			throw new Exception ( 'Xml loading error: ' . $e->getMessage () );
		}
	}
	
	public function getIds() {
		$result = array ();
		foreach ( $this->_elements as $row ) {
			array_push ( $result, $row->id );
		}
		return $result;
	}
	
	public function find($ids) {
		if (is_array ( $ids )) {
			$result = array ();
			foreach ( $this->_elements as $row ) {
				if (isset ( $ids [$row->id] ))
					array_push ( $result, $row );
			}
			return $result;
		}
	}
	
	public function __construct($file_name) {
		$this->_file = $file_name;
		$this->_loadFromXmlFile ( $file_name );
	}
}